<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring Boot 3.x + MyBatis Plus 示例</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        body {
            padding-top: 5rem;
        }
        .card {
            margin-bottom: 1.5rem;
        }
        .table-container {
            max-height: 400px;
            overflow-y: auto;
        }
        .footer {
            margin-top: 3rem;
            padding: 1.5rem 0;
            border-top: 1px solid #eaeaea;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
        <div class="container">
            <a class="navbar-brand" href="#">Spring Boot 3.x + MyBatis Plus 示例</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/mybatis-plus/user-management">用户管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/swagger-ui.html}" target="_blank">API文档</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/h2-console}" target="_blank">H2控制台</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <main class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="jumbotron">
                    <h1 class="display-4">Spring Boot 3.x + MyBatis Plus 示例</h1>
                    <p class="lead">这个示例展示了如何在Spring Boot 3.x应用中集成和使用MyBatis Plus进行数据库操作。</p>
                    <hr class="my-4">
                    <p>示例包含完整的CRUD操作、分页查询、多表关联查询等MyBatis Plus核心功能。</p>
                    <p class="lead">
                        <a class="btn btn-primary btn-lg" th:href="@{/swagger-ui.html}" role="button" target="_blank">查看API文档</a>
                    </p>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h4>系统概览</h4>
                    </div>
                    <div class="card-body">
                        <ul class="list-group">
                            <li class="list-group-item">
                                <strong>总用户数：</strong>
                                <span class="badge bg-primary" th:text="${totalUsers}">0</span>
                            </li>
                            <li class="list-group-item">
                                <strong>数据库类型：</strong>
                                H2 内存数据库
                            </li>
                            <li class="list-group-item">
                                <strong>MyBatis Plus版本：</strong>
                                3.5.5
                            </li>
                            <li class="list-group-item">
                                <strong>Spring Boot版本：</strong>
                                3.2.5
                            </li>
                        </ul>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h4>快速链接</h4>
                    </div>
                    <div class="card-body">
                        <div class="list-group">
                            <a href="/mybatis-plus/user-management" class="list-group-item list-group-item-action">
                                用户管理页面
                            </a>
                            <a href="/mybatis-plus/api/users" class="list-group-item list-group-item-action" target="_blank">
                                用户API列表
                            </a>
                            <a href="/mybatis-plus/api/users/with-roles" class="list-group-item list-group-item-action" target="_blank">
                                用户及角色API列表
                            </a>
                            <a href="/mybatis-plus/swagger-ui.html" class="list-group-item list-group-item-action" target="_blank">
                                Swagger API文档
                            </a>
                            <a href="/mybatis-plus/h2-console" class="list-group-item list-group-item-action" target="_blank">
                                H2数据库控制台
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h4>用户列表</h4>
                    </div>
                    <div class="card-body">
                        <div class="table-container">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>用户名</th>
                                        <th>邮箱</th>
                                        <th>电话</th>
                                        <th>状态</th>
                                        <th>创建时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:each="user : ${users}">
                                        <td th:text="${user.id}"></td>
                                        <td th:text="${user.username}"></td>
                                        <td th:text="${user.email}"></td>
                                        <td th:text="${user.phone}"></td>
                                        <td>
                                            <span class="badge" th:classappend="${user.status == 1 ? 'bg-success' : 'bg-danger'}">
                                                <span th:text="${user.status == 1 ? '启用' : '禁用'}"></span>
                                            </span>
                                        </td>
                                        <td th:text="${user.createTime}"></td>
                                        <td>
                                            <a th:href="@{/api/users/{id}(id=${user.id})}" class="btn btn-sm btn-info" target="_blank">详情</a>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h4>用户及角色关联信息</h4>
                    </div>
                    <div class="card-body">
                        <div class="table-container">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>用户ID</th>
                                        <th>用户名</th>
                                        <th>拥有角色</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:each="user : ${usersWithRoles}">
                                        <td th:text="${user.id}"></td>
                                        <td th:text="${user.username}"></td>
                                        <td>
                                            <div th:each="role, roleStat : ${user.roles}">
                                                <span class="badge bg-secondary" th:text="${role.name}"></span>
                                                <span th:if="!${roleStat.last}">, </span>
                                            </div>
                                        </td>
                                        <td>
                                            <a th:href="@{/api/users/{id}/with-roles(id=${user.id})}" class="btn btn-sm btn-info" target="_blank">查看角色</a>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h4>功能演示</h4>
                    </div>
                    <div class="card-body">
                        <div class="accordion" id="featureAccordion">
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne">
                                        基础CRUD操作
                                    </button>
                                </h2>
                                <div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#featureAccordion">
                                    <div class="accordion-body">
                                        <p>示例实现了完整的用户增删改查功能，包括：</p>
                                        <ul>
                                            <li>查询所有用户</li>
                                            <li>根据ID查询用户</li>
                                            <li>创建新用户</li>
                                            <li>更新用户信息</li>
                                            <li>删除用户</li>
                                        </ul>
                                        <p>所有操作都可以通过API文档页面进行测试。</p>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo">
                                        高级查询功能
                                    </button>
                                </h2>
                                <div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#featureAccordion">
                                    <div class="accordion-body">
                                        <p>示例实现了MyBatis Plus的高级查询功能，包括：</p>
                                        <ul>
                                            <li>分页查询用户</li>
                                            <li>根据角色ID查询用户</li>
                                            <li>多表关联查询（用户和角色）</li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree">
                                        事务管理
                                    </button>
                                </h2>
                                <div id="collapseThree" class="accordion-collapse collapse" data-bs-parent="#featureAccordion">
                                    <div class="accordion-body">
                                        <p>示例使用Spring的声明式事务管理，确保数据操作的一致性：</p>
                                        <ul>
                                            <li>创建用户并分配角色（事务保证）</li>
                                            <li>更新用户角色（事务保证）</li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <footer class="footer bg-light">
        <div class="container text-center">
            <span class="text-muted">Spring Boot 3.x + MyBatis Plus 示例 &copy; 2024</span>
        </div>
    </footer>

    <script>
        // 为表格添加简单的排序功能
        document.addEventListener('DOMContentLoaded', function() {
            const tables = document.querySelectorAll('table');
            tables.forEach(table => {
                const headers = table.querySelectorAll('th');
                headers.forEach((header, index) => {
                    header.style.cursor = 'pointer';
                    header.addEventListener('click', () => {
                        sortTable(table, index);
                    });
                });
            });
        });

        function sortTable(table, columnIndex) {
            const tbody = table.querySelector('tbody');
            const rows = Array.from(tbody.querySelectorAll('tr'));
            const isAscending = table.getAttribute('data-sort-order') !== 'asc';
            
            // 排序行
            const sortedRows = rows.sort((a, b) => {
                const aValue = a.cells[columnIndex].textContent.trim();
                const bValue = b.cells[columnIndex].textContent.trim();
                
                // 尝试数字比较
                if (!isNaN(aValue) && !isNaN(bValue)) {
                    return isAscending ? parseFloat(aValue) - parseFloat(bValue) : parseFloat(bValue) - parseFloat(aValue);
                }
                
                // 字符串比较
                return isAscending ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
            });
            
            // 更新排序状态
            table.setAttribute('data-sort-order', isAscending ? 'asc' : 'desc');
            
            // 重新添加行
            sortedRows.forEach(row => tbody.appendChild(row));
        }
    </script>
</body>
</html>